package com.yunquan.ohana.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.core.content.res.ResourcesCompat;

import com.yunquan.ohana.R;

public class CircleTextView extends View {
    private Paint paint;
    private int circleColor;
    private Paint outerCirclePaint;
    private int outerCircleColor;
    private Paint textPaint;
    private String circleText;
    private int circleTextColor;
    private float circleTextSize;
    private float circleRadius;
    private float outerCircleRadius;

    public CircleTextView(Context context) {
        this(context, null);
    }

    public CircleTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 设置 layout_margin
     * @param left 左边距
     * @param top 上边距
     * @param right 右边距
     * @param bottom 下边距
     */
    public void setLayoutMargin(int left, int top, int right, int bottom, int w, int h) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new FrameLayout.LayoutParams(w, h);
        }
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            marginLayoutParams.setMargins(left, top, right, bottom);
            setLayoutParams(marginLayoutParams);
        }
    }

    private void init(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        circleColor = typedArray.getColor(R.styleable.CircleView_circleColor, Color.RED);
        outerCircleColor = typedArray.getColor(R.styleable.CircleView_outerCircleColor, Color.GRAY);
        circleText = typedArray.getString(R.styleable.CircleView_circleText);
        circleTextColor = typedArray.getColor(R.styleable.CircleView_circleTextColor, Color.WHITE);
        circleTextSize = typedArray.getDimension(R.styleable.CircleView_circleTextSize, 20);
        circleRadius = typedArray.getDimension(R.styleable.CircleView_circleRadius, 100);
        outerCircleRadius = typedArray.getDimension(R.styleable.CircleView_outerCircleRadius, circleRadius * 1.2f);
        typedArray.recycle();

        paint = new Paint();
        paint.setColor(circleColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        outerCirclePaint = new Paint();
        outerCirclePaint.setColor(outerCircleColor);
        outerCirclePaint.setStyle(Paint.Style.FILL);
        outerCirclePaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(circleTextColor);
        textPaint.setTextSize(circleTextSize);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setAntiAlias(true);

        Typeface customTypeface = ResourcesCompat.getFont(context, R.font.montserrat_regular);
        textPaint.setTypeface(customTypeface);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float minSize = Math.min(w, h);
        circleRadius = Math.min(circleRadius, minSize / 2f);
        outerCircleRadius = Math.min(outerCircleRadius, minSize / 2f);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int centerX = width / 2;
        int centerY = height / 2;

        canvas.drawCircle(centerX, centerY, outerCircleRadius, outerCirclePaint);
        canvas.drawCircle(centerX, centerY, circleRadius, paint);

        if (circleText != null) {
            Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            float baseline = centerY - (fontMetrics.top + fontMetrics.bottom) / 2 - 2;
            canvas.drawText(circleText, centerX, baseline, textPaint);
        }
    }

    public void setCircleColor(int color) {
        circleColor = color;
        paint.setColor(circleColor);
        invalidate();
    }

    public void setOuterCircleColor(int color) {
        outerCircleColor = color;
        outerCirclePaint.setColor(outerCircleColor);
        invalidate();
    }

    public void setCircleText(String text) {
        circleText = text;
        invalidate();
    }

    public void setCircleTextColor(int color) {
        circleTextColor = color;
        textPaint.setColor(circleTextColor);
        invalidate();
    }

    public void setCircleTextSize(float size) {
        circleTextSize = size;
        textPaint.setTextSize(circleTextSize);
        invalidate();
    }

    public void setCircleRadius(float radius) {
        circleRadius = radius;
        invalidate();
    }

    public void setOuterCircleRadius(float radius) {
        outerCircleRadius = radius;
        invalidate();
    }

    public float getCircleTextSize() {
        return circleTextSize;
    }

    public float getCircleRadius() {
        return circleRadius;
    }

    public float getOuterCircleRadius() {
        return outerCircleRadius;
    }
}
